package com.hup.utils.swing.view.viewTable2.model;

import com.hup.utils.commons.DateBuilder;
import com.hup.utils.swing.customView.viewTable2.ViewTableColumn;
import com.hup.utils.swing.customView.viewTable2.ViewTableColumn.VDateTextRenderer;
import com.hup.utils.swing.customView.viewTable2.ViewTableColumn.VDatetimeTextRenderer;
import com.hup.utils.swing.customView.viewTable2.ViewTableColumn.VSummaryHandler;
import com.hup.utils.swing.customView.viewTable2.ViewTableColumn.VTextRenderer;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.swing.SwingConstants;
import java.util.Date;
import java.util.List;

/**
 * @author hugan
 * @date 2024/5/28
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class VMockPo {

    private Long id;

    @ViewTableColumn(name = "自动序号", isIndexColumn = true, order = 100,
            valueAlignment = SwingConstants.RIGHT,
            paddingX = 10,
            width = 50)
    private Integer numTable;

    @ViewTableColumn(name = "序号", headerAlignment = SwingConstants.LEFT,
            valueAlignment = SwingConstants.RIGHT,
            paddingX = 10,
            fixWidth = 50)
    private Integer num;

    @ViewTableColumn(name = "隐藏", visible = false,
            editable = true,
            valueAlignment = SwingConstants.LEFT,
            paddingX = 2,
            fixWidth = 50)
    private Integer hide;

    @ViewTableColumn(name = "名称", editable = true,
            editPreview = true,
            headerAlignment = SwingConstants.RIGHT, valueAlignment = SwingConstants.LEFT,
            paddingX = 10,
            headerTooltip = "名称")
    private String name;

    @ViewTableColumn(name = "时长", textRender = MySpanRenderer.class,
            editable = true,
            width = 100,
            headerAlignment = SwingConstants.RIGHT,//test
            //headerAlignment = SwingConstants.LEFT,//test
            summaryHandler = MySumSpanHandler.class
            //summaryHandler = NullSummaryHandler.class//test
    )
    private Integer span;

    private String spanStr;

    @ViewTableColumn(name = "是否", order = 2, editable = true)
    private Boolean hadOne;

    @ViewTableColumn(name = "日期", width = 100, textRender = VDateTextRenderer.class,
            editable = true,
            summaryHandler = MySumHandler.class,
            headerTooltip = "日期"
            //summaryHandler = NullSummaryHandler.class//test
    )
    private Date date;

    @ViewTableColumn(name = "时间", width = 180, textRender = VDatetimeTextRenderer.class)
    private Date datetime;

    private String ignore;

    public static class MySpanRenderer implements VTextRenderer<VMockPo> {
        @Override
        public String get(VMockPo row, Object value) {
            if (value == null) return "";
            return DateBuilder.spanChar(((int) value) * 1000);
        }
    }

    public static class MySumHandler implements VSummaryHandler<VMockPo> {
        @Override
        public String get(List<VMockPo> allData) {
            return allData.size() + "天";
        }
    }

    public static class MySumSpanHandler implements VSummaryHandler<VMockPo> {
        @Override
        public String get(List<VMockPo> allData) {
            int sum = allData.stream().mapToInt(o -> o.getSpan() == null ? 0 : o.getSpan()).sum();
            return DateBuilder.spanChar(sum * 1000L);
        }
    }

}
